// pages/wait/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: 0,
    fatherPage: {},
    appointmentDate: {
      Y: 0,
      M: 0,
      D: 0,
      H: 0,
      Min: 0
    },
    readyDate: [],
    oneDay:{
      Y: "",
      M: "",
      D: "",
      W: ""
    },
    dateDetails:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // display a week to appoint ment
    let oneWeek = [];
    // time now
    let dateNow = new Date();
    // time header
    let date = new Date();
    // get pages
    let pages = getCurrentPages();

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        })
      },
    });

    if (dateNow.getHours() > 20) {
      date.setDate(date.getDate() + 1);
    }
    for (let i = 0; i < 7; i++) {
      let w = date.getDay();
      if (w == 0) w = "周日"
      else if (w == 1) w = "周一"
      else if (w == 2) w = "周二"
      else if (w == 3) w = "周三"
      else if (w == 4) w = "周四"
      else if (w == 5) w = "周五"
      else if (w == 6) w = "周六"
      let oneDay = {
        Y: date.getFullYear(),
        M: date.getMonth() + 1,
        D: date.getDate(),
        W: w
      };
      oneWeek.push(oneDay);
      date.setDate(date.getDate() + 1);
    }
    that.setData({
      readyDate: oneWeek,
      "appointmentDate.Y": oneWeek[0].Y,
      "appointmentDate.M": oneWeek[0].M,
      "appointmentDate.D": oneWeek[0].D,
      "appointmentDate.H": 0,
      "appointmentDate.Min": 0,
      fatherPage: pages[pages.length - 2]
    });
    that.timeHelper(that.data.appointmentDate.D == dateNow.getDate());
    that.data.fatherPage.setData({
      "appointment.isActive": false
    })
  },

  selDate: function (event){
    let that = this;
    // time now
    let dateNow = new Date();
    let details = that.data.details;
    that.setData({
      "appointmentDate.D": event.currentTarget.dataset.dateTime.D
    });
    that.timeHelper(that.data.appointmentDate.D == dateNow.getDate());
  },

  selDetail: function (event){
    let that = this;
    if (event.currentTarget.dataset.detailTime.remain > 0) {
      that.setData({
        "appointmentDate.H": event.currentTarget.dataset.detailTime.H,
        "appointmentDate.Min": event.currentTarget.dataset.detailTime.Min
      })
    }
  },

  checkout: function (){
    let that = this;
    that.data.fatherPage.setData({
      "appointment.isActive": true,
      "appointment.Y": that.data.appointmentDate.Y,
      "appointment.M": that.data.appointmentDate.M,
      "appointment.D": that.data.appointmentDate.D,
      "appointment.H": that.data.appointmentDate.H,
      "appointment.Min": that.data.appointmentDate.Min,
    })
    wx.navigateBack({
      delta: 1
    })
    console.log(that.data.appointmentDate);
  },

  timeHelper: function (isToday){
    let that = this;
    // time detail
    let tempDate = new Date();
    // time details
    let details = [];
    // time now
    let dateNow = new Date();
    // get time detail
    if (dateNow.getHours() > 20) {
      tempDate.setDate(tempDate.getDate() + 1);
    }
    tempDate.setHours(9);
    tempDate.setMinutes(0);
    for (let i = 0; i < 24; i++) {
      let detail = {
        H: tempDate.getHours(),
        Min: tempDate.getMinutes(),
        remain: isToday ? (tempDate.getTime() > dateNow.getTime() ? 15 : 0) : 15
      }
      details.push(detail);
      tempDate.setMinutes(tempDate.getMinutes() + 30);
    }
    that.setData({
      dateDetails: details
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})